'use strict';

const Service = require('egg').Service;

class PosPhotoService extends Service {
    /**
     * 图片入库
     * @param {PosPhotoDto.createDto} params 
     * @returns 
     */
    async create(params) {
        try {
            let createParams = [];
            params.list.map(item => {
                createParams.push({
                    folderId: params.folderId,
                    ...item
                })
            })
            await this.ctx.model.PosPhoto.bulkCreate(createParams, { updateOnDuplicate: ["url", "fullUrl", "folderId", "companyId"] });
            return this.ctx.helper.ormSuccess();
        } catch (err) {
            return this.ctx.helper.ormError(err);
        }
    }

    /**
   * 分页查询
   * @param {PosPhotoDto.pageDto} params 
   * @returns 
   */
    async page(params) {
        try {
            const { Op } = this.app.Sequelize;
            const offset = (Number(params.pi || 1) - 1) * Number(params.ps || 100);
            let queryCriteria = {
                folderId: params.folderId,
            };
            const query = {
                limit: Number(params.ps || 100),
                offset,
                where: queryCriteria,
            };
            const res = await this.ctx.model.PosPhoto.findAndCountAll(query);
            return this.ctx.helper.ormSuccess(res);
        } catch (err) {
            return this.ctx.helper.ormError(err);
        }
    }
}

module.exports = PosPhotoService;
